//
//  JwSkinExer.m
//  JwPack
//
//  Created by 陈警卫 on 2023/1/12.
//

#import "JwSkinExer.h"
#import "NSObject+JwSkin.h"
#import "UIColor+JwSkin.h"

#define kJwSkinExerKey @"JwSkinExerKey"

@interface JwSkinExer ()

/// MARK: 当前的主题 默认default
@property (nonatomic, strong) NSString *jw_currentSkin;

@end

@implementation JwSkinExer

/// MARK: 单例
+ (JwSkinExer *)shared {
    static JwSkinExer *sharedObject;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedObject = [[JwSkinExer alloc] initPrivate];
    });
    return sharedObject;
}

- (instancetype)init {
    return [[self class] shared];
}

- (instancetype)initPrivate {
    self = [super init];
    if (self) {
        [self setupData];
    }
    return self;
}

- (void)setupData {
    // 设置当前主题
    self.jw_currentSkin = [[NSUserDefaults standardUserDefaults] objectForKey:kJwSkinExerKey];
    if (self.jw_currentSkin == nil) {
        self.jw_currentSkin = @"default";
        [[NSUserDefaults standardUserDefaults] setObject:self.jw_currentSkin forKey:kJwSkinExerKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

/// MARK: 设置主题
- (void)jw_setCurrentSkinKey:(NSString *)key {
    self.jw_currentSkin = key;
    
    [[NSUserDefaults standardUserDefaults] setObject:self.jw_currentSkin forKey:kJwSkinExerKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kJwSkinNotiKey object:nil userInfo:nil];
}

@end


@implementation JwSkinModel

/// MARK: 获取当前颜色
- (UIColor *)jw_color {
    NSString *colorString = self.param[kJwSkinExer.jw_currentSkin];
    if (colorString == nil) {
        colorString = self.param[@"default"] ?: @"000000";
    }
    UIColor *color = [UIColor jw_skinColorHexString:colorString];
    return color;
}

/**
 sel: _cmd
 param: 颜色参数 @{@"default": @"color", @"dark": @"color"}
 */
+ (JwSkinModel *)jw_modelWithSel:(SEL)sel param:(NSDictionary *)param {
    JwSkinModel *model = [[JwSkinModel alloc] init];
    model.key = NSStringFromSelector(sel);
    model.param = param;
    return model;
}


@end
